package com.hns.can.base.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysAnnouncementEntity;
import com.hns.can.base.entity.SysAnnouncementReadEntity;
import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.base.service.SysSystemAnnounceService;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.domain.ReturnResult;
import com.hns.can.common.web.BaseController;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.pub.UtilString;

/**
 * 系统公告controller
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("SystemAnnounce")
public class SysSystemAnnounceController extends BaseController {
	/**
	 * 系统公告service
	 */
	@Autowired
	private SysSystemAnnounceService sysSystemAnnounceService;
	
	/**
	 * 查询系统公告
	 * @param title 标题
	 * @return 系统公告列表
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("getSystemAnnounce")
	@ResponseBody
	public Object getSystemAnnounce(@RequestParam("announTitle")String title){
		SysUserEntity user = this.getCurrentUser().getUser();
		Pagination page = sysSystemAnnounceService.getSystemAnnounce(title, user.getCorpId());
		GridPage grid = new GridPage(page);
		return new ReturnResult(grid);
	}
	
	/**
	 * 添加系统公告
	 * @param entity 系统公告对象
	 * @return 操作返回对象
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("addSystemAnnounce")
	@ResponseBody
	public Object addSystemAnnounce(SysAnnouncementEntity entity){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = format.format(new Date());
		entity.setAnnounId(UtilString.getUUID());
		entity.setAnnounTime(dateString);
		entity.setCorpId(this.getCurrentUser().getUser().getCorpId());
		sysSystemAnnounceService.addSystemAnnounce(entity);
		return this.getReturnResult();
	}
	
	/**
	 * 删除系统公告
	 * @param docIds 公告id
	 * @return 操作返回对象
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("deleteSystemAnnounce")
	@ResponseBody
	public Object deleteSystemAnnounce(@RequestParam("docIds")String[] docIds){
		sysSystemAnnounceService.deleteSystemAnnounce(docIds);
		return this.getReturnResult();
	}
		
	/**
	 * 获取指定公告
	 * @param announId 公告id
	 * @return 指定公告实体
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("getAnnounceContent")
	@ResponseBody
	public Object getAnnounceContent(@RequestParam("announId")String announId){
		SysAnnouncementEntity announceContent = sysSystemAnnounceService.getAnnounceContent(announId);
		return this.getReturnResult(announceContent);
	}
	
	/**
	 * 保存已读信息
	 * @param entity 公告已读状态实体
	 * @return 操作返回对象
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("saveReadAnnounce")
	@ResponseBody
	public Object saveReadAnnounce(SysAnnouncementReadEntity entity){
		SysUserEntity user = this.getCurrentUser().getUser();
		entity.setReadId(UtilString.getUUID());
		entity.setCorpId(user.getCorpId());
		entity.setUserId(user.getId());
		sysSystemAnnounceService.saveReadAnnounce(entity);
		return this.getReturnResult();
	}
	
	
}
